package com.chaodev.base.controller;

import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.ExplanationLineDto;
import com.chaodev.base.model.ExplanationLine;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ExplanationLineService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("explanationLine")
@Api(tags = "导览解说线路")
public class ExplanationLineController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private ExplanationLineService explanationLineService;

    @PostMapping(value = "/addExplanationLine")
    @ApiOperation(value = "新增导览解说线路")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result addExplanationLine(@RequestBody ExplanationLine explanationLine, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            explanationLine.setCreateUser(userInfo.getUserId());
            if (explanationLineService.addExplanationLine(explanationLine)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLine.toString(), "新增导览解说线路", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/editExplanationLine")
    @ApiOperation(value = "修改导览解说线路")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result editExplanationLine(@RequestBody ExplanationLine explanationLine, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (explanationLine.getId() == null || "".equals(explanationLine.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            explanationLine.setUpdateUser(userInfo.getUserId());
            if (explanationLineService.editExplanationLine(explanationLine)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLine.toString(), "修改导览解说线路", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/saveExplanationLine")
    @ApiOperation(value = "保存导览解说线路")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result saveExplanationLine(@RequestBody ExplanationLine explanationLine, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            explanationLine.setCreateUser(userInfo.getUserId());
            if (explanationLineService.saveExplanationLine(explanationLine)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLine.toString(), "保存导览解说线路", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/removeExplanationLine")
    @ApiOperation(value = "删除导览解说线路")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result removeExplanationLine(@RequestBody ExplanationLine explanationLine, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (explanationLine.getId() == null || "".equals(explanationLine.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if (explanationLineService.removeExplanationLine(explanationLine)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLine.toString(), "删除导览解说线路", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationLineList")
    @ApiOperation(value = "查询导览解说线路列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationLineList(@RequestBody ExplanationLineDto explanationLineDto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            result.setData(explanationLineService.getExplanationLineList(explanationLineDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLineDto.toString(), "查询导览解说线路列表", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationLineById")
    @ApiOperation(value = "查询单条导览解说线路")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationLineById(@RequestBody ExplanationLine explanationLine, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (explanationLine.getId() == null || "".equals(explanationLine.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            result.setData(explanationLineService.getExplanationLineById(explanationLine));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLine.toString(), "查询单条导览解说线路", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationLinePage")
    @ApiOperation(value = "查询导览解说线路分页")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationLinePage(@RequestBody ExplanationLineDto explanationLineDto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if(explanationLineDto.getPage() == null || explanationLineDto.getPage() == 0
                    || explanationLineDto.getRows() == null || explanationLineDto.getRows() == 0){
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少页码或条数");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            result.setData(explanationLineService.getExplanationLinePage(explanationLineDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLineDto.toString(), "查询导览解说线路分页", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationLinePageNoAuth")
    @ApiOperation(value = "查询导览解说线路分页")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationLinePageNoAuth(@RequestBody ExplanationLineDto explanationLineDto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if(explanationLineDto.getPage() == null || explanationLineDto.getPage() == 0
                    || explanationLineDto.getRows() == null || explanationLineDto.getRows() == 0){
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少页码或条数");
                return result;
            }

            result.setData(explanationLineService.getExplanationLinePage(explanationLineDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLineDto.toString(), "导览小程序查询导览解说线路分页", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationLineByIdNoAuth")
    @ApiOperation(value = "查询单条导览解说线路")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationLineByIdNoAuth(@RequestBody ExplanationLine explanationLine, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (explanationLine.getId() == null || "".equals(explanationLine.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            result.setData(explanationLineService.getExplanationLineById(explanationLine));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationLine.toString(), "导览小程序查询单条导览解说线路", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

}
